package com.drivemanage.service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.drivemanage.dao.TestDao;
import com.drivemanage.entry.CarInfo;
import com.drivemanage.entry.DriveResult;
import com.drivemanage.entry.TestInfo;
import com.drivemanage.utils.OpenUtils;

/**
 * @author cuij
 * @version 创建时间：2017-5-15 上午11:26:15
 *          程序的简单说明
 */
@Service
@Transactional
public class TestServiceImpl implements TestService {

    @Resource
    private TestDao testdao;

    @Override
    public DriveResult getCarBond() {
        List<TestInfo> carInfos = testdao.qryCarInfo();
        System.out.println(carInfos);
        String bianhao = "";
        String pinpai = "";
        String pinpailist = "";
        String tag = "A";
        Map<String, String> bondMap = new HashMap<String, String>();
        for (int j = 0; j < carInfos.size(); j++) {
            // bianhao = carInfos.get(j).getBianhao();
            pinpai = carInfos.get(j).getPinpai();
            if (!bianhao.equals(tag)) {
                bondMap.put(tag, pinpailist);
                // tag = carInfos.get(j).getBianhao();
                pinpailist = "";
            }
            pinpailist += pinpai + ",";
            if ("Z".equals(tag)) {
                bondMap.put(tag, pinpailist);
            }
            System.out.println(pinpailist);
        }
        return new DriveResult(DriveResult.SUCCESS_CODE, "查询成功", bondMap);
    }

    @Override
    public DriveResult getCarLink() {
        List<TestInfo> carName = testdao.getCarName();
        String carlist1 = "";
        String carlist2 = "";
        String carlist3 = "";
        String caradd1 = "";
        String caradd2 = "";
        // 1.第一轮遍历
        for (int i = 0; i < carName.size(); i++) {
            String name = carName.get(i).getPinpai();
            List<TestInfo> carTypes = testdao.getCarType(name);
            carlist1 = "{name:'" + name + "',cityList:[";
            // 2.第二轮遍历
            for (int j = 0; j < carTypes.size(); j++) {
                String carType = carTypes.get(j).getChexi();
                carlist2 = "{name:'" + carType + "',areaList:[";
                Map paramMap = new HashMap();
                paramMap.put("name", name);
                paramMap.put("carType", carType);
                List<TestInfo> carDetails = testdao.getCarDetail(paramMap);
                for (int k = 0; k < carDetails.size(); k++) {
                    String carDetail = carDetails.get(k).getChexingname();
                    if (k == carDetails.size() - 1) {
                        carlist3 += "'" + carDetail + "'";
                    }
                    else {
                        carlist3 += "'" + carDetail + "',";
                    }
                }
                if (j == carTypes.size() - 1) {
                    caradd1 += carlist2 + carlist3 + "]}";
                    carlist3 = "";
                }
                else {
                    caradd1 += carlist2 + carlist3 + "]},";
                    carlist3 = "";
                }
            }
            caradd2 += carlist1 + caradd1 + "]},";
            caradd1 = "";
        }
        System.out.println("最终结果" + caradd2);
        return new DriveResult(DriveResult.SUCCESS_CODE, "成功", caradd2);
    }

    /**
     * 上传图片
     */
    @Override
    public DriveResult changeAndImg(MultipartFile carpic, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        byte[] logoBytes = null;
        if (!carpic.isEmpty()) {
            try {
                logoBytes = carpic.getBytes();
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File("sofapic")));
                stream.write(logoBytes);
                stream.close();
            }
            catch (Exception e) {
                return new DriveResult(DriveResult.ERROR_CODE, "上传图片出错!请重新上传", "");
            }
        }
        else {
            return new DriveResult(DriveResult.ERROR_CODE, "上传的图片有误!请重新选择", "");
        }
        if (OpenUtils.isEmpty(logoBytes) || logoBytes.length > 15 * 1024 * 1024) {
            return new DriveResult(DriveResult.ERROR_CODE, "上传图片过大!请重新选择", "");
        }
        String carId = OpenUtils.createCarID();
        CarInfo carInfo = new CarInfo(carId, 0, logoBytes, "", "", "", "", "", "", "", "", "", "", "", 0, "", "", "",
                "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
        carInfo.setIs_valid("0");// 设置为失效
        testdao.addCarEmg(carInfo);
        return new DriveResult(DriveResult.SUCCESS_CODE, "图片上传成功", carId);
    }

    /**
     * 从老库中查询信息
     */
    @Override
    public DriveResult queryAllInfo(String pinpai, String chexi, String chexingming) {
        Map paramMap = new HashMap();
        paramMap.put("pinpai", pinpai);
        paramMap.put("chexi", chexi);
        paramMap.put("chexingming", chexingming);
        List<TestInfo> carInfos = testdao.queryCarInfo(paramMap);
        return new DriveResult(DriveResult.SUCCESS_CODE, "图片上传成功", carInfos);
    }
}
